﻿using System;
using System.Collections.Generic;
using System.Text;
using GroupChat2013.Entity;
using GroupChat2013.Common;
using System.Web;
using GroupChat2013.Logic.Message;

namespace GroupChat2013.Interface
{
    /// <summary>
    /// 信息发送接受接口
    /// </summary>
    public abstract class IMessage
    {
        /// <summary>
        /// 图片下载基类
        /// </summary>
        public AbstractImageUpload m_ImageUpload;

        /// <summary>
        /// 接受信息
        /// </summary>
        /// <param name="cid">cid</param>
        /// <returns>聊天信息</returns>
        public abstract List<MessageInfo> ReceiverMessage(string cid);

        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="receiverId">消息主体</param>
        /// <param name="content">消息内附带的图片信息</param>
        /// <param name="cid">用户标识</param>
        /// <returns></returns>
        public abstract List<MessageInfo> SendMessage(MessageContent message, List<string> imageList, string cid);

        /// <summary>
        /// 将json格式的content解析成对象
        /// </summary>
        /// <param name="messageList">聊天信息数组</param>
        public void ParseMessageContent(List<MessageInfo> messageList)
        {
            messageList.ForEach(x =>
            {
                x.MessageContent = JsonConvert.Parse<MessageContent>(x.content);

                if (x.MessageContent != null)
                {
                    DateTime dt = DateTime.Now;
                    DateTime.TryParse(x.createTime, out dt);
                    x.MessageContent.CreateTime = dt;

                    x.MessageContent.Content = HttpUtility.UrlDecode(x.MessageContent.Content);
                    //x.MessageContent.FromId = x.cid;
                    //x.MessageContent.FromName = x.senderName;
                }
            });
        }
    }
}
